package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IProductDao;
import com.cn.wanxi.dao.impl.ProductDaoImpl;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;

public class ProductServiceImpl implements IProductService {
    IProductDao productDao = new ProductDaoImpl();
    @Override
    public List<ProductModel> getProductList(Map<String, Object> map) {
        int page = (int) map.get("page");
        int type = (int)map.get("product_type");
        List<ProductModel> productList = null;
        if (page==0 && type==0) {
            try {
                Jedis jedis = new Jedis();
                if (jedis.exists("product_id")) {
                    productList = productDao.getProductListByRedis();
                } else {
                    productList = productDao.getProductList(map);
                    productDao.setProductListInRedis(productList);
                }
            } catch (Exception e) {
                productList = productDao.getProductList(map);
            }
        }else
            productList = productDao.getProductList(map);
        return productList;
    }

    @Override
    public ProductModel getProduct(int id) {
        return productDao.getProduct(id);
    }

    @Override
    public int addProduct(ProductModel productModel) {
        return productDao.addProduct(productModel);
    }

    @Override
    public int deleteProduct(int id) {
        return productDao.deleteProduct(id);
    }

    @Override
    public int updateProduct(ProductModel productModel) {
        return productDao.updateProduct(productModel);
    }

    @Override
    public PageModel<ProductModel> getProducts(Map<String,Object> map) {
        PageModel pager = new PageModel();
        pager.setList(productDao.getProducts(map));
        pager.setTotal(productDao.getProductCount(map));
        return pager;
    }

    @Override
    public int updateShow(int id,int show) {
        ProductModel product = new ProductModel();
        product.setId(id);
        product.setIsShow(show);
        return productDao.updateShow(product);
    }

    @Override
    public long getProductListCount(Map<String, Object> map) {
        return productDao.getProductListCount(map);
    }
}
